<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-form-item label="监督任务" v-bind="validateInfos.wsupTaskName">
        <a-select v-model:value="formData.wsupTaskName" :options="wsupTaskNameOptions" placeholder="请输入监督任务" :disabled="disabled"></a-select>
      </a-form-item>
      <a-form-item label="方案名称" v-bind="validateInfos.schemeName">
        <a-input v-model:value="formData.schemeName" placeholder="请输入方案名称" :disabled="disabled"></a-input>
      </a-form-item>
      <a-form-item label="方案编辑人" v-bind="validateInfos.editor">
        <a-input v-model:value="formData.editor" placeholder="请输入方案编辑人" :disabled="disabled"></a-input>
      </a-form-item>
      <a-form-item label="编辑日期" v-bind="validateInfos.editDate">
        <a-date-picker v-model:value="formData.editDate" placeholder="请输入编辑日期" :disabled="disabled" :format="dateFormat" />
      </a-form-item>
      <a-form-item label="方案附件" v-bind="validateInfos.schemeAtta">
        <JUpload v-model:value="formData.schemeAtta" :disabled="disabled"></JUpload>
      </a-form-item>
      <a-form-item label="审核人" v-bind="validateInfos.auditId">
        <a-select v-model:value="formData.auditId" :options="optionReceiver" @change="handleSelectChange" placeholder="请选择审核人" mode="multiple" :disabled="disabled"></a-select>
      </a-form-item>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
import { ref, reactive, defineExpose, nextTick, defineProps, computed } from 'vue';
import JUpload from '/@/components/Form/src/jeecg/components/JUpload/JUpload.vue';
import { useMessage } from '/@/hooks/web/useMessage';
import { saveOrUpdate, getReceiverList,getSupervisionTasksList } from '../../supervisionPlan/SupervisionPlanList.api';
import { Form } from 'ant-design-vue';
import { useUserStore } from '/@/store/modules/user';
const { userInfo }:any = useUserStore();
import dayjs from 'dayjs';

const props = defineProps({
  formDisabled: { type: Boolean, default: false },
  formData: { type: Object, default: () => { } },
  formBpm: { type: Boolean, default: true },
});

const optionReceiver = ref<any>([]);
const wsupTaskNameOptions = ref<any>([]);
const formRef = ref();
const useForm = Form.useForm;
const emit = defineEmits(['register', 'ok']);
const dateFormat = 'YYYY-MM-DD'
const formData = reactive<Record<string, any>>({
  auditCause: "",
  auditId: undefined,
  auditTime: "",
  auditor: "",
  createBy: "",
  createTime: "",
  editDate: dayjs(),
  editor: userInfo.realname,
  editorId: "",
  id: "",
  issueTime: "",
  issuer: "",
  receiveId: "",
  receiver: "",
  returnCause: "",
  returnTime: "",
  returner: "",
  schemeAtta: "",
  schemeName: "",
  status: "",
  synFlag: "",
  sysOrgCode: "",
  updateBy: "",
  updateTime: "",
  wsupTaskId: "",
  wsupTaskName: ""
});
const { createMessage } = useMessage();
const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
const confirmLoading = ref<boolean>(false);
//表单验证
const validatorRules = {
  wsupTaskName: [{ required: true, message: '监督任务不能为空！', trigger: 'blur' }],
  schemeName: [{ required: true, message: '方案名称不能为空！', trigger: 'blur' }],
  editor: [{ required: true, message: '方案编辑人不能为空！', trigger: 'blur' }],
  editDate: [{ required: true, message: '编辑日期不能为空！', trigger: 'blur' }],
  schemeAtta: [{ required: true, message: '方案附件不能为空！', trigger: 'blur' }],
  auditId: [{ required: true, message: '审核人不能为空！', trigger: 'blur' }],
};
const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

const getReceiverListOptions = async ()=>{
  const res = await getReceiverList();
  if(res.success&&res.result.records.length>0){
    res.result.records.forEach(element => {
      optionReceiver.value.push({
        value:element.id,
        label:element.realname,
      }) 
    });    
  }  
}
const getSupervisionTasksOptions = async () =>{
  const res = await getSupervisionTasksList();
  if(res.success&&res.result.records.length>0){
    res.result.records.forEach(element => {
      wsupTaskNameOptions.value.push({
        value:element.id,
        label:element.spotEntName,
      }) 
    });    
  }   
}

getReceiverListOptions();
getSupervisionTasksOptions();
// 表单禁用
const disabled = computed(() => {
  if (props.formBpm === true) {
    if (props.formData.disabled === false) {
      return false;
    } else {
      return true;
    }
  }
  return props.formDisabled;
});

const handleSelectChange = (_:any,option:any) =>{
  if(option.length>0){
    let auditorText = option.map((item:any)=>item.label)
    formData.auditor = auditorText.join(',');
  }
}

/**
 * 新增
 */
function add() {
  edit({});
}

/**
 * 编辑
 */
function edit(record) {
  nextTick(async () => {
    resetFields();
    //赋值
    Object.assign(formData, record);
    if(formData.editDate){
      formData.editDate=dayjs(formData.editDate)
    }
    if(formData.auditId){
      formData.auditId=JSON.parse(formData.auditId);
    }   
  });
}
/**
 * 提交数据
 */
async function submitForm() {
  // 触发表单验证
  await validate();
  confirmLoading.value = true;
  const isUpdate = ref<boolean>(false);
  //时间格式化
 
  if (formData.id != null && formData.id != '') {
    isUpdate.value = true;
  }
  if(formData.auditId&&formData.auditId.length>0){
    formData.auditId=JSON.stringify(formData.auditId);
  }
  let model = formData;
  await saveOrUpdate(model, isUpdate.value)
    .then((res) => {
      if (res.success) {
        createMessage.success(res.message);
        emit('ok');
      } else {
        createMessage.warning(res.message);
      }
    })
    .finally(() => {
      confirmLoading.value = false;
    });
}

defineExpose({
  add,
  edit,
  submitForm,
  formData,
});
</script>

<style lang="less" scoped>
.antd-modal-form {
  overflow-y: auto;
  padding: 24px 24px 24px 24px;
}
.ant-picker{
  width:100%;
}

.signBox {
  display: flex;
  width: 100%;
}

.signBoxLabel {
  margin-right: 50px;
  display: flex;
  padding-left: 20px;
}

.signImg {
  width: 200px;
}
</style>
